package com.ai.consumer;

import com.ai.common.KafkaConstant;
import com.ai.service.CallService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class AiConsumer {

    @Resource
    private CallService callService;

    @KafkaListener(topics = KafkaConstant.AI_ITEM_NAME)
    public void consumer(ConsumerRecord<String, String> consumerRecord, Acknowledgment ack) {
        try {
            String itemName = consumerRecord.value();
            System.out.println("监听到Kafka：" + itemName);
            callService.exec(itemName);
        } catch (Exception e) {
            System.err.println("消费kafka出现错误");
        } finally {
            ack.acknowledge();
        }

    }
}
